package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.common.constants.WxMpConstant;
import com.ibeeking.found.msg.rest.service.IWxAutoReplyService;
import com.ibeeking.found.msg.service.common.dos.WxAutoReplyDO;
import com.ibeeking.found.msg.service.common.param.WxAutoReplyParam;
import com.ibeeking.found.msg.service.common.query.WxAutoReplyPageQuery;
import com.ibeeking.found.msg.service.common.vo.WxAutoReplyVO;
import com.ibeeking.found.msg.service.mapper.WxAutoReplyMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class WxAutoReplyServiceImpl extends ServiceImpl<WxAutoReplyMapper, WxAutoReplyDO> implements IWxAutoReplyService {

    @Resource
    private WxAutoReplyMapper wxAutoReplyMapper;


    @Override
    public Page<WxAutoReplyVO> pageList(WxAutoReplyPageQuery wxAutoReplyPageQuery) {
        WxAutoReplyDO wxAutoReplyDO = BeanUtil.convertBean(wxAutoReplyPageQuery,WxAutoReplyDO.class);
        Page<WxAutoReplyDO> page = new Page<>(wxAutoReplyPageQuery.getPageNum(),wxAutoReplyPageQuery.getPageSize());
        return BeanUtil.convertPage(this.wxAutoReplyMapper.selectPage(page, Wrappers.query(wxAutoReplyDO)),WxAutoReplyVO.class);
    }

    @Override
    public WxAutoReplyVO view(Long id) {
        WxAutoReplyDO wxAutoReplyDO = this.wxAutoReplyMapper.selectById(id);
        return BeanUtil.convertBean(wxAutoReplyDO,WxAutoReplyVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WxAutoReplyParam wxAutoReplyParam) {
        WxAutoReplyDO wxAutoReply = BeanUtil.convertBean(wxAutoReplyParam,WxAutoReplyDO.class);
        wxAutoReply.setCreateTime(LocalDateTime.now());
        wxAutoReply.setModifyTime(LocalDateTime.now());
        //this.jude(wxAutoReply);
        return this.wxAutoReplyMapper.insert(wxAutoReply)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(WxAutoReplyParam wxAutoReplyParam) {
        WxAutoReplyDO wxAutoReply = BeanUtil.convertBean(wxAutoReplyParam,WxAutoReplyDO.class);
        wxAutoReply.setModifyTime(LocalDateTime.now());
        this.jude(wxAutoReply);
        return this.wxAutoReplyMapper.updateById(wxAutoReply)>0;
    }

    @Override
    public Boolean del(Long id) {
        return this.wxAutoReplyMapper.deleteById(id)>0;
    }

    /**
     * 校验参数
     * @param wxAutoReply
     */
    @Override
    public void jude(WxAutoReplyDO wxAutoReply){
        if(WxMpConstant.WX_AUTO_REPLY_TYPE_2.equals(wxAutoReply.getType())){
            QueryWrapper<WxAutoReplyDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(WxAutoReplyDO::getReqType,wxAutoReply.getReqType());
            List<WxAutoReplyDO> list = this.wxAutoReplyMapper.selectList(queryWrapper);
            if(wxAutoReply.getId()!=null){
                if(list != null && list.size() == 1){
                    if(!list.get(0).getId().equals(wxAutoReply.getId())){
                        throw new RuntimeException("请求消息类型重复");
                    }
                }
                if(list != null && list.size()>1){
                    throw new RuntimeException("请求消息类型重复");
                }
            }
        }
        if(WxMpConstant.WX_AUTO_REPLY_TYPE_3.equals(wxAutoReply.getType())){
            QueryWrapper<WxAutoReplyDO> queryWrapper = new QueryWrapper<>();
                    queryWrapper.lambda().eq(WxAutoReplyDO::getReqKey,wxAutoReply.getReqKey())
                    .eq(WxAutoReplyDO::getRepType,wxAutoReply.getRepMate());
            List<WxAutoReplyDO> list = this.wxAutoReplyMapper.selectList(queryWrapper);
            if(list != null && list.size() == 1){
                if(!list.get(0).getId().equals(wxAutoReply.getId())){
                    throw new RuntimeException("关键词重复");
                }
            }
            if(list != null && list.size()>1){
                throw new RuntimeException("关键词重复");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(Object o) {
        return null;
    }
}

